package academic.emailClient.view;

import java.awt.Color;
import java.awt.FlowLayout;
import java.io.File;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

public class TemplateView {
	static final int MAIN_FRAME_MINIMUM_HEIGHT = 600;
	static final int MAIN_FRAME_MINIMUM_WIDTH = 600;
	static final int GAP_BETWEEN_COMPONENTS = 10;
	private JPanel templatesPanel;
	private JComboBox templatescb;
	private JTextArea textArea;
	VelocityTemplate templateVelocity;
	
	/**
	 * TemplateView constructor
	 * @throws Exception 
	 */
	public TemplateView () throws Exception{
		String[] items = getAllFilesNamesInTemplatesFolder();
		templatescb = new JComboBox(items);
		templatesPanel= new JPanel(new FlowLayout(FlowLayout.CENTER, GAP_BETWEEN_COMPONENTS, GAP_BETWEEN_COMPONENTS));
		templatesPanel.setBackground(Color.WHITE);
		templateVelocity = new VelocityTemplate();
		textArea = new JTextArea();
	}

	/**
	 * get Template Cb Selection
	 * @return
	 */
	public String getTemplateCbSelection(){
		return templatescb.getSelectedItem().toString();		
	}
	
	/**
	 * returns file names of all the files in email folders
	 * @return
	 */
	private String[] getAllFilesNamesInTemplatesFolder(){
		String[] names = new String[100];
		names[0] = "Select Template";
		int i = 1;
		File folder = new File("Templates");
		for (File  f: folder.listFiles()){
			 names[i] = f.getName();
			 i++;
		}
		return names;
	}
	
	/** Create the Read email panel
	 * @throws Exception 
	 * @throws ParseErrorException 
	 * @throws ResourceNotFoundException */
	public JPanel createTemplateContentPanel() throws ResourceNotFoundException, ParseErrorException, Exception
	{
		JLabel bodyLabel = new JLabel(templateVelocity.getTemplateString(getTemplateCbSelection()));
		JPanel panel = new JPanel();
		panel.add(bodyLabel);
		return panel;
	}
	
	/**
	 * Set Text in Text Area
	 * @param text
	 */
	public void setTextArea(String text){
		textArea.setText(text);
	}
	
	/**
	 * get Manage Template ComboBox
	 * @return
	 */
	public JComboBox getManageTemplateComboBox(){
		return templatescb;		
	}	
}
